package com.example.消息过期.ttlmessage;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 */
@Configuration
public class DelayConfig {

    public static final String DELAY_QUEUE = "delay_queue";
    public static final String DELAY_EXCHANGE = "delay_exchange";

    @Bean
    Queue delayQueue() {
        return new Queue(DELAY_QUEUE, true, false, false);
    }

    /**
     * 定义一个直连交换机
     *
     * @return
     */
    @Bean
    DirectExchange delayExchange() {
        //1. 交换机的名称
        //2。交换机是否持久化
        //3. 如果没有与之绑定的队列，是否删除交换机
        return new DirectExchange(DELAY_EXCHANGE, true, false);
    }

    /**
     * 这个定义是将交换机和队列绑定起来
     *
     * @return
     */
    @Bean
    Binding deplyBinding() {
        return BindingBuilder
            .bind(delayQueue()).to(delayExchange()).with(DELAY_QUEUE);
    }

}
